﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;

namespace ProjectStudy.文件读写
{
    public partial class 文件读写 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        //读小文件
        private string ReadSmallFile1(string filePath)
        {
            string str = File.ReadAllText(filePath,Encoding.Default);
            return str;
        }

        //如果文件不存在，则创建；存在则覆盖
        private void WriteFile1(string filePath, string str)
        {
            File.WriteAllText(filePath, str,Encoding.Default);
        }

        //读小文件
        private string[] ReadSmallFile2(string filePath)
        {
            string[] str = File.ReadAllLines(filePath,Encoding.Default);
            return str;
        }

        //如果文件不存在，则创建；存在则覆盖
        private void WriteFile2(string filePath,string[] str)
        {
            File.WriteAllLines(filePath, str, Encoding.Default);
        }

        //读大文件
        private string ReadBigFile(string filePath)
        {
            string str = "";
            using(FileStream fs=new FileStream(filePath,FileMode.Open))
            {
              byte[] bt=new byte[1024*1024*2]; //创建2M缓冲区
              int length = fs.Read(bt, 0, bt.Length);
              if (length > 0)
              {
                  str = Encoding.Default.GetString(bt);
              }
            }
            return str;
        }

        /// <summary>
        /// 文件写入
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="str">要写入的内容</param>
        /// <param name="isAppend">是否追加到文件尾部</param>
        /// <param name="isNewLine">是否换行</param>
        private void WriteFile3(string filePath, string[] str, bool isAppend,bool isNewLine)
        { 
          using(StreamWriter sw=new StreamWriter(filePath,isAppend,Encoding.Default))
          {
              foreach (string line in str)
              {
                  if (isNewLine)
                  {
                      sw.WriteLine();
                      sw.Write(line); 
                  }
                  else
                  {
                      sw.Write(line);
                  }
              }
          }
        }

        protected void btn1_Click(object sender, EventArgs e)
        {
            string str = "第一个文本域";
            WriteFile1("D:/学习笔记/项目学习/项目学习/FileUpload/文件读写/1.txt",str);
            LoadData();
        }

        protected void btn2_Click(object sender, EventArgs e)
        {
            string[] str = { "第二个文本域" };
            WriteFile2("D:/学习笔记/项目学习/项目学习/FileUpload/文件读写/1.txt", str);
            LoadData();
        }

        protected void btn3_Click(object sender, EventArgs e)
        {
            string[] str = { "第三个文本域" };
            WriteFile3("D:/学习笔记/项目学习/项目学习/FileUpload/文件读写/1.txt", str, true, true);
            LoadData();
        }

        private void LoadData()
        {
            tb1.Text = ReadSmallFile1("D:/学习笔记/项目学习/项目学习/FileUpload/文件读写/1.txt");

            string[] s = ReadSmallFile2("D:/学习笔记/项目学习/项目学习/FileUpload/文件读写/1.txt");
            string s2 = "";
            for (int i = 0; i < s.Length; i++)
            {
                s2 = s2 + s[i] + "\n";
            }
            tb2.Text = s2;

            tb3.Text = ReadBigFile("D:/学习笔记/项目学习/项目学习/FileUpload/文件读写/1.txt");
        }

      
    }
}